<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiDirectory
{
	public static function subdir($path = './', $recursive = FALSE, $exclude = array('.', '..', '.settings'))
	{
		// validate path
		if (! file_exists($path))
		{
			return array();
		}
		
		// append path
		$files[] = $path;	
			
		// opens up a directory handle 
		$dh = opendir($path);
		
		// itterate each filesystem object
		while (false !== ($filename = readdir($dh))) 
		{
			// check exclude rules
			if (! in_array($filename, $exclude)) 
		    {
			    // glue path with filename
			    $filename = $path . '/' .$filename;
			    
			    if (is_dir($filename))
			    {			    	
			    	// append filesystem object
					$files[] = $filename;
					
					// if is dir and recursive=TRUE
					if ($recursive === TRUE)
			        {
			        	// scan subdir 
			        	$_files = self::subdir($filename, $recursive, $exclude);
			        	
			        	// remove duplicate item
			        	unset($_files[0]);
			        	
			        	// merge result
			        	$files = array_merge($files, $_files);		        	
			        }
			    }
		    }				
		}
		
		// return
		return $files;				
	}
	
	public static function scan($path = './', $recursive = FALSE, $exclude = array('.', '..', 'index.php', 'index.html', '.project', '.htaccess', '.buildpath', '.settings'))
	{
		// validate path
		if (! file_exists($path))
		{
			return array();
		}
		
		// append path
		$files[] = $path;	
			
		// opens up a directory handle 
		$dh = opendir($path);
		
		// itterate each filesystem object
		while (false !== ($filename = readdir($dh))) 
		{
			// check exclude rules
			if (! in_array($filename, $exclude)) 
		    {
		    	// glue path with filename
		    	$filename = $path . '/' .$filename;
		    	
		    	// append filesystem object
				$files[] = $filename;
				
				// if is dir and recursive=TRUE
				if (is_dir($filename) && ($recursive === TRUE))
		        {
		        	// scan subdir 
		        	$_files = self::scan($filename, $recursive, $exclude);
		        	
		        	// remove duplicate item
		        	unset($_files[0]);
		        	
		        	// merge result
		        	$files = array_merge($files, $_files);		        	
		        }				
		    }				
		}
		
		// return
		return $files;
	}
	
	public static function tree($path = './', $recursive = FALSE, $exclude = array('.', '..', 'index.php', 'index.html', '.project', '.htaccess', '.buildpath', '.settings'))
	{
		$files = array();
		
		// validate path
		if (! file_exists($path))
		{
			return $files;
		}
		
		// append path
		$files[$path] = NULL;	
		
		// opens up a directory handle 
		$dh = opendir($path);
		
		// itterate each filesystem object
		while (false !== ($filename = readdir($dh))) 
		{
			// check exclude rules
			if (! in_array($filename, $exclude)) 
		    {
		    	// glue path with filename
		    	$_filename = $path . '/' .$filename;
		    	
		    	// append filesystem object
				$files[$path][$_filename] = $filename;
				
				// if is dir and recursive=TRUE
				if (is_dir($_filename) && ($recursive === TRUE))
		        {
		        	// scan subdir 
		        	$_files = self::tree($_filename, $recursive, $exclude);
		        	
		        	// remove duplicate item
		        	unset($_files[0]);
		        	
		        	// merge result
		        	$files[$path][$_filename] = $_files[$_filename];	        	
		        }				
		    }				
		}
		
		// return
		return $files;
	}

}

/* End of file RtiDirectory.php */
/* Location: ./system/helpers/RtiDirectory.php */
